﻿<!doctype html>
<html>
<head>
<#include "/part_meta.ftl">
<title>
<#if searchWord?? && searchWord?length gt 0>
   	搜"${searchWord}"
<#elseif category??>
	${category.name}
<#else>
	全部商品
</#if>-迈小众
</title>
<link href="${st.resDomain}/css/style.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/store.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/autostyle.css" rel="stylesheet" type="text/css"> 
<style>
header .store .tab a { width: 28%;}
</style>
</head>

<body>
<form id="listForm" style="display:none;">
	<input type="hidden" id="storeId" name="store.storeId" value="${store.storeId}">
	<input type="hidden" id="catId" name="category.catId" value="${category.catId}">
	<input type="hidden" id="searchWord" name="searchWord" value="${searchWord}">
	<input type="hidden" id="orderField" name="orderField" value="<#if orderField??>${orderField}<#else>sort</#if>">
	<input type="hidden" id="orderType" name="orderType" value="DESC">
	<input type="hidden" id="nowPage" name="nowPage" value="1">
</form>
<!--头部-->
<header>
  <div class="boxw store">
    <div class="header">
      <span class="f_left"><a href="javascript:history.back();" class="back"></a></span>
      <span class="title">
	  	<#if searchWord?? && searchWord?length gt 0>
		   	搜商品"<span style="display:inline;">${searchWord}</span>"
		<#elseif category??>
			${category.name}
		<#else>
			全部商品
		</#if>
	  </span>
      <span class="f_right"><a class="more"></a></span>
    </div>
    <div class="tab">
    	<a id="rank_sort">默认</a>
        <a id="rank_dealMonth">销量</a>
        <a id="rank_price">价格</a>
        <a class="st"><i></i></a>
    </div>
    <div class="more">
      <a href="${st.domain}/search" class="icon-search">搜索</a>
      <a href="${st.domain}/category" class="icon-fl">类目</a>
      <a href="${st.userDomain}/shoppingCart" class="icon-car">购物车</a>
      <a href="${st.userDomain}/" class="icon-user">个人中心</a>
      <a href="${st.domain}/" class="icon-home">主页</a>
    </div>
  </div>
</header>
<!--头部 END-->

<div class="main boxw" style="padding-top:5rem;">
  <ul class="goodlist f_clear" id="sgList"></ul>
  <div id="loadTip" class="listTip">点击加载更多</div>
</div>
<div id="goodsItemTmp" style="display:none;">
	<li style="-{style}-"><a href="${st.domain}/storeGoods/detail/-{id}-"><span><img src="${st.resDomain}/images/loading.gif" data="-{pic}-" dtype="tmp" /><i>-{underStock}-</i></span>
        <h3>-{name}-</h3>
        <h4><strong>￥-{price}-</strong><del id="originalPrize_-{id}-" style="display:none;">-{originalPrice}-</del>/-{unit}-</h4>
      </a></li>
</div>
<script src="${st.resDomain}/scripts/jquery-1.9.1.min.js"></script>
<script src="${st.resDomain}/scripts/com.js"></script>
<script>
	var loadSgListPath = g_domain + "/storeGoods/list/json";
	var isLoadging = false;
	var pageInfo = {};
	var $sgList = $("#sgList");
	var $loadTip = $("#loadTip");
	var goodsItemTmp = $("#goodsItemTmp").html();
	var searchWord = $("#searchWord").val();
	$("#rank_" + $("#orderField").val()).addClass("selected");
	loadSgList();
	function loadSgList() {
		isLoadging = true;
		$loadTip.show().html("正在加载...");
		$.getJSON(loadSgListPath, $("#listForm").serialize(), function(data){
			isLoadging = false;
			$loadTip.html("点击加载更多");
			if (data.status !== 0) {
				showMsg("加载失败!", 'error');
				return;
			}
			var sgList = data.result;
			if (!sgList) {
				$loadTip.html("没有相关商品");
				return;
			}
			for (var i = 0; i < sgList.length; i++) {
				var storeGoods = sgList[i];
				var itemTmp = goodsItemTmp;
				var name = storeGoods.title + (storeGoods.subTitle?"<span style='color:red;display:inline-block;margin-left:3px;'>" + storeGoods.subTitle + "</span>":"");
				var underStock = "";
				if (searchWord) {
					// 搜索关键字变红
					name = name.replace(searchWord,"<span style='color:red;display:inline-block;'>" + searchWord + "</span>")
				}
				if (parseInt(storeGoods.stock) <= storeGoods.stockEnd) {
					underStock = "<i>已售完</i>";
				}
				var style = "";
				if (i%2 == 0) {
					style = "clear:both;";
				} 
				itemTmp = itemTmp
				.replace(/\-\{style\}\-/g, style)
				.replace(/\-\{id\}\-/g, storeGoods.storeGoodsId)
				.replace(/\-\{pic\}\-/g, storeGoods.pic)
				.replace(/\-\{name\}\-/g, name)
				.replace(/\-\{price\}\-/g, storeGoods.price)
				.replace(/\-\{originalPrice\}\-/g, storeGoods.originalPrice)
				.replace(/\-\{unit\}\-/g, storeGoods.unit)
				.replace(/\-\{underStock\}\-/g, underStock)
				.replace('dtype="tmp"', "");
				$sgList.append(itemTmp);
				if (storeGoods.atDiscount) {
					$("#originalPrize_" + storeGoods.storeGoodsId).show();
				}
			}
			pageInfo = data.pageInfo;
			if (pageInfo.nowPage == pageInfo.totalPage) {
				$loadTip.hide();
			}
			// 触发滚动事件,使视窗中的图片显示
			$(document).trigger('showimg');
		});
	}
	
	// 加载更多
	$loadTip.click(function() {
		if (!isLoadging && pageInfo.nowPage < pageInfo.totalPage) {
			$("#nowPage").val(pageInfo.nowPage + 1);
			isLoadging?void(0):loadSgList();
		}
	});
	
	// 排序
	$("[id^='rank_']").click(function() {
		$(".tab a").removeClass("selected");
		$(this).addClass("selected");
		var destField = $(this).attr("id").replace("rank_","");
		if (destField == "price" && $("#orderField").val() == destField) {
			$("#orderType").val($("#orderType").val() == "DESC" ? "ASC":"DESC");
			$(this).children("span").removeClass().
			addClass($("#orderType").val() == "DESC" ? "down":"up");
		} else {
			$("#orderField").val(destField);
			$("#orderType").val("DESC");
			if (destField == "price") {
				$(this).children("span").removeClass().addClass("down");
			}
		}
		$sgList.html("");
		$("#nowPage").val(1);
		loadSgList();
	});
</script>
</body>
</html>
